{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SSA Object for IVOA protocols (v1.2)\n",
    "    v1.2 -- Updated to Spectral Data Model v2.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# imports\n",
    "from astropy.io.votable.tree import VOTableFile, Resource, Field, Info, Param\n",
    "from astropy.io.votable.tree import Table as voTable\n",
    "from astropy.io.votable import from_table\n",
    "\n",
    "from astropy.coordinates import SkyCoord\n",
    "\n",
    "import specdb\n",
    "from specdb import specdb as ssdb\n",
    "from specdb import ssa as spdb_ssa"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fiddling with QueryData and VOTables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "tst_file = specdb.__path__[0]+'/tests/files/IGMspec_DB_v02_debug.hdf5'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using /Users/xavier/local/Python/specdb/specdb/tests/files/IGMspec_DB_v02_debug.hdf5 for the DB file\n",
      "Available groups: [u'BOSS_DR12', u'HD-LLS_DR1', u'GGG', u'SDSS_DR7']\n",
      "Database is igmspec\n",
      "Created on 2017-Jan-07\n"
     ]
    }
   ],
   "source": [
    "reload(ssdb)\n",
    "igmsp = ssdb.IgmSpec(db_file=tst_file, verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generate VOTable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "reload(spdb_ssa)\n",
    "ssai = spdb_ssa.SSAInterface(igmsp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your search yielded 1 match[es] within radius=0.001 deg\n"
     ]
    }
   ],
   "source": [
    "votable = ssai.querydata('0.0019,17.7737', SIZE=1e-3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "votable.to_xml('tmp.xml')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Here is what it looks like\n",
    "    <?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
    "    <!-- Produced with astropy.io.votable version 1.3\n",
    "         http://www.astropy.org/ -->\n",
    "    <VOTABLE version=\"1.2\" xmlns=\"http://www.ivoa.net/xml/VOTable/v1.2\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.ivoa.net/xml/VOTable/v1.2\">\n",
    "     <RESOURCE type=\"results\">\n",
    "      <INFO ID=\"QUERY_STATUS\" name=\"QUERY_STATUS\" value=\"OK\"/>\n",
    "      <INFO ID=\"SERVICE_PROTOCOL\" name=\"SERVICE_PROTOCOL\" value=\"1.1\"/>\n",
    "      <INFO ID=\"REQUEST\" name=\"REQUEST\" value=\"queryData\"/>\n",
    "      <INFO ID=\"serviceName\" name=\"serviceName\" value=\"ssap\"/>\n",
    "      <INFO ID=\"FORMAT\" name=\"FORMAT\" value=\"HDF5\"/>\n",
    "      <INFO ID=\"SIZE\" name=\"SIZE\" value=\"0.001\"/>\n",
    "      <INFO ID=\"POS\" name=\"POS\" value=\"0.0019,17.7737\"/>\n",
    "      <TABLE>\n",
    "       <FIELD ID=\"DataModel\" arraysize=\"12\" datatype=\"char\" name=\"DataModel\" utype=\"ssa:Dataset.DataModel\"/>\n",
    "       <FIELD ID=\"DatasetType\" arraysize=\"8\" datatype=\"char\" name=\"DatasetType\" utype=\"ssa:Dataset.Type\"/>\n",
    "       <FIELD ID=\"Title\" arraysize=\"28\" datatype=\"char\" name=\"Title\" ucd=\"meta.title;meta.dataset\" utype=\"ssa:DataID.Title\"/>\n",
    "       <FIELD ID=\"Instrument\" arraysize=\"4\" datatype=\"char\" name=\"Instrument\" ucd=\"meta.id;instr\" utype=\"ssa:DataID.Title\"/>\n",
    "       <FIELD ID=\"Publisher\" arraysize=\"6\" datatype=\"char\" name=\"Publisher\" ucd=\" meta.curation\" utype=\"ssa:Curation.Publisher\"/>\n",
    "       <FIELD ID=\"SpaceFrameName\" arraysize=\"4\" datatype=\"char\" name=\"SpaceFrameName\" utype=\"ssa:CoordSys.SpaceFrame.Name\"/>\n",
    "       <FIELD ID=\"SpaceFrameEquinox\" datatype=\"double\" name=\"SpaceFrameEquinox\" ucd=\"time.equinox;pos.frame\" unit=\"yr\" utype=\"ssa:CoordSys.SpaceFrame.Equinox\"/>\n",
    "       <FIELD ID=\"TargetName\" arraysize=\"11\" datatype=\"char\" name=\"TargetName\" ucd=\"meta.id;src\" utype=\"ssa:Target.Name\"/>\n",
    "       <FIELD ID=\"FluxAxisUcd\" arraysize=\"18\" datatype=\"char\" name=\"FluxAxisUcd\" utype=\"ssa:Char.FluxAxis.ucd\"/>\n",
    "       <FIELD ID=\"FluxAxisUnit\" arraysize=\"26\" datatype=\"char\" name=\"FluxAxisUnit\" utype=\"ssa:Char.FluxAxis.unit\"/>\n",
    "       <FIELD ID=\"FluxAxisCalibration\" arraysize=\"8\" datatype=\"char\" name=\"FluxAxisCalibration\" utype=\"ssa:Char.FluxAxis.Calibration\"/>\n",
    "       <FIELD ID=\"SpectralAxisUcd\" arraysize=\"5\" datatype=\"char\" name=\"SpectralAxisUcd\" utype=\"ssa:Char.SpectralAxis.ucd\"/>\n",
    "       <FIELD ID=\"SpectralAxisUnit\" arraysize=\"8\" datatype=\"char\" name=\"SpectralAxisUnit\" utype=\"ssa:Char.SpectralAxis.unit\"/>\n",
    "       <FIELD ID=\"TimeLocation\" datatype=\"double\" name=\"TimeLocation\" ucd=\"time.epoch\" unit=\"d\" utype=\"ssa:Char.TimeAxis.Coverage.Location.Value\"/>\n",
    "       <FIELD ID=\"SpatialLocation\" arraysize=\"2\" datatype=\"double\" name=\"SpatialLocation\" ucd=\"pos.eq\" unit=\"deg\" utype=\"ssa:Char.SpatialAxis.Coverage.Location.Value\"/>\n",
    "       <FIELD ID=\"SpectralLocation\" datatype=\"double\" name=\"SpectralLocation\" ucd=\"instr.bandpass\" unit=\"AA\" utype=\"ssa:Char.Spectral.Coverage.Location.Value\"/>\n",
    "       <FIELD ID=\"SpectralBoundsExtent\" datatype=\"double\" name=\"SpectralBoundsExtent\" ucd=\"instr.bandwidth\" unit=\"AA\" utype=\"ssa:Char.Spectral.Coverage.Bounds.Extent\"/>\n",
    "       <FIELD ID=\"SpectralBoundsStart\" datatype=\"double\" name=\"SpectralBoundsStart\" ucd=\"em.wl;stat.min\" unit=\"AA\" utype=\"ssa:Char.Spectral.Coverage.Bounds.Start\"/>\n",
    "       <FIELD ID=\"SpectralBoundsStop\" datatype=\"double\" name=\"SpectralBoundsStop\" ucd=\"em.wl;stat.max\" unit=\"AA\" utype=\"ssa:Char.Spectral.Coverage.Bounds.Stop\"/>\n",
    "       <DATA>\n",
    "        <TABLEDATA>\n",
    "         <TR>\n",
    "          <TD>Spectrum-2.0</TD>\n",
    "          <TD>Spectrum</TD>\n",
    "          <TD>BOSS_DR12: BOSS DR12 Quasars</TD>\n",
    "          <TD>BOSS</TD>\n",
    "          <TD>specdb</TD>\n",
    "          <TD>ICRS</TD>\n",
    "          <TD>2000</TD>\n",
    "          <TD>BOSS_DR12_0</TD>\n",
    "          <TD>phot.fluDens;em.wl</TD>\n",
    "          <TD>erg s**(-1) angstrom**(-1)</TD>\n",
    "          <TD>ABSOLUTE</TD>\n",
    "          <TD>em.wl</TD>\n",
    "          <TD>Angstrom</TD>\n",
    "          <TD>56238</TD>\n",
    "          <TD>0.001898285183756343 17.773739129913793</TD>\n",
    "          <TD>6947.298095703125</TD>\n",
    "          <TD>6684.68310546875</TD>\n",
    "          <TD>3604.95654296875</TD>\n",
    "          <TD>10289.6396484375</TD>\n",
    "         </TR>\n",
    "        </TABLEDATA>\n",
    "       </DATA>\n",
    "      </TABLE>\n",
    "     </RESOURCE>\n",
    "    </VOTABLE>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## METADATA Query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "reload(spdb_ssa)\n",
    "ssai = spdb_ssa.SSAInterface(igmsp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: W03: ?:?:?: W03: Implicitly generating an ID from a name 'INPUT:POS' -> 'INPUT_POS' [astropy.io.votable.exceptions]\n",
      "WARNING: W03: ?:?:?: W03: Implicitly generating an ID from a name 'INPUT:SIZE' -> 'INPUT_SIZE' [astropy.io.votable.exceptions]\n",
      "WARNING: W03: ?:?:?: W03: Implicitly generating an ID from a name 'INPUT:BAND' -> 'INPUT_BAND' [astropy.io.votable.exceptions]\n",
      "WARNING: W03: ?:?:?: W03: Implicitly generating an ID from a name 'INPUT:TIME' -> 'INPUT_TIME' [astropy.io.votable.exceptions]\n",
      "WARNING: W03: ?:?:?: W03: Implicitly generating an ID from a name 'INPUT:FORMAT' -> 'INPUT_FORMAT' [astropy.io.votable.exceptions]\n"
     ]
    }
   ],
   "source": [
    "votable = ssai.querydata(FORMAT='METADATA')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "votable.to_xml('meta.xml')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Here is the output:\n",
    "    <?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
    "    <!-- Produced with astropy.io.votable version 1.3\n",
    "         http://www.astropy.org/ -->\n",
    "    <VOTABLE version=\"1.2\" xmlns=\"http://www.ivoa.net/xml/VOTable/v1.2\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.ivoa.net/xml/VOTable/v1.2\">\n",
    "     <RESOURCE type=\"results\">\n",
    "      <PARAM ID=\"INPUT_POS\" arraysize=\"*\" datatype=\"char\" name=\"INPUT:POS\" value=\"\">\n",
    "       <DESCRIPTION>\n",
    "        The center of the region of interest. The coordinate values are\n",
    "        specified in list format (comma separated) in decimal degrees with\n",
    "        no embedded white space followed by an optional coord. system.\n",
    "        Allowed systems are (ICRS) and the default is ICRS.\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <PARAM ID=\"INPUT_SIZE\" datatype=\"double\" name=\"INPUT:SIZE\" unit=\"deg\" value=\"0.1\">\n",
    "       <DESCRIPTION>\n",
    "        The radius of the circular region of interest in decimal\n",
    "        degrees.Default sized radius is 0.001 degrees\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <PARAM ID=\"INPUT_BAND\" arraysize=\"*\" datatype=\"char\" name=\"INPUT:BAND\" value=\"ALL\">\n",
    "       <DESCRIPTION>\n",
    "        Not currently implemented\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <PARAM ID=\"INPUT_TIME\" arraysize=\"*\" datatype=\"char\" name=\"INPUT:TIME\" value=\"\">\n",
    "       <DESCRIPTION>\n",
    "        Not currently implemented\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <PARAM ID=\"INPUT_FORMAT\" arraysize=\"*\" datatype=\"char\" name=\"INPUT:FORMAT\" value=\"ALL\">\n",
    "       <DESCRIPTION>\n",
    "        Desired format of retrieved data.  Allowed values are HDF5,\n",
    "        METADATA\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <PARAM ID=\"DataModel\" arraysize=\"*\" datatype=\"char\" name=\"OUTPUT:DataModel\" utype=\"ssa:Dataset.DataModel\" value=\"\">\n",
    "       <DESCRIPTION>\n",
    "        Datamodel name and version\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <PARAM ID=\"DatasetType\" arraysize=\"*\" datatype=\"char\" name=\"OUTPUT:DatasetType\" utype=\"ssa:Dataset.Type\" value=\"Spectrum\">\n",
    "       <DESCRIPTION>\n",
    "        Dataset type\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <PARAM ID=\"Title\" arraysize=\"*\" datatype=\"char\" name=\"OUTPUT:Title\" ucd=\"meta.title;meta.dataset\" utype=\"ssa:DataID.Title\" value=\"\">\n",
    "       <DESCRIPTION>\n",
    "        Dataset Title\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <PARAM ID=\"Instrument\" arraysize=\"*\" datatype=\"char\" name=\"OUTPUT:Instrument\" ucd=\"meta.id;instr\" utype=\"ssa:DataID.Title\" value=\"\">\n",
    "       <DESCRIPTION>\n",
    "        Instrument name\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <PARAM ID=\"Publisher\" arraysize=\"*\" datatype=\"char\" name=\"OUTPUT:Publisher\" ucd=\" meta.curation\" utype=\"ssa:Curation.Publisher\" value=\"\"/>\n",
    "      <PARAM ID=\"SpaceFrameName\" arraysize=\"*\" datatype=\"char\" name=\"OUTPUT:SpaceFrameName\" utype=\"ssa:CoordSys.SpaceFrame.Name\" value=\"\">\n",
    "       <DESCRIPTION>\n",
    "        Spatial coordinate frame name\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <PARAM ID=\"SpaceFrameEquinox\" datatype=\"double\" name=\"OUTPUT:SpaceFrameEquinox\" ucd=\"time.equinox;pos.frame\" unit=\"yr\" utype=\"ssa:CoordSys.SpaceFrame.Equinox\" value=\"0\"/>\n",
    "      <PARAM ID=\"TargetName\" arraysize=\"1\" datatype=\"char\" name=\"OUTPUT:TargetName\" ucd=\"meta.id;src\" utype=\"ssa:Target.Name\" value=\"\"/>\n",
    "      <PARAM ID=\"FluxAxisUcd\" arraysize=\"*\" datatype=\"char\" name=\"OUTPUT:FluxAxisUcd\" utype=\"ssa:Char.FluxAxis.ucd\" value=\"\"/>\n",
    "      <PARAM ID=\"FluxAxisUnit\" arraysize=\"*\" datatype=\"char\" name=\"OUTPUT:FluxAxisUnit\" utype=\"ssa:Char.FluxAxis.unit\" value=\"\"/>\n",
    "      <PARAM ID=\"FluxAxisCalibration\" arraysize=\"*\" datatype=\"char\" name=\"OUTPUT:FluxAxisCalibration\" utype=\"ssa:Char.FluxAxis.Calibration\" value=\"\"/>\n",
    "      <PARAM ID=\"SpectralAxisUcd\" arraysize=\"*\" datatype=\"char\" name=\"OUTPUT:SpectralAxisUcd\" utype=\"ssa:Char.SpectralAxis.ucd\" value=\"\"/>\n",
    "      <PARAM ID=\"SpectralAxisUnit\" arraysize=\"*\" datatype=\"char\" name=\"OUTPUT:SpectralAxisUnit\" utype=\"ssa:Char.SpectralAxis.unit\" value=\"\"/>\n",
    "      <PARAM ID=\"TimeLocation\" datatype=\"double\" name=\"OUTPUT:TimeLocation\" ucd=\"time.epoch\" unit=\"d\" utype=\"ssa:Char.TimeAxis.Coverage.Location.Value\" value=\"0\">\n",
    "       <DESCRIPTION>\n",
    "        Estimated UT day of observations\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <PARAM ID=\"SpatialLocation\" arraysize=\"2\" datatype=\"double\" name=\"OUTPUT:SpatialLocation\" ucd=\"pos.eq\" unit=\"deg\" utype=\"ssa:Char.SpatialAxis.Coverage.Location.Value\" value=\"0\">\n",
    "       <DESCRIPTION>\n",
    "        Spatial Position\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <PARAM ID=\"SpectralLocation\" datatype=\"double\" name=\"OUTPUT:SpectralLocation\" ucd=\"instr.bandpass\" unit=\"AA\" utype=\"ssa:Char.Spectral.Coverage.Location.Value\" value=\"0\">\n",
    "       <DESCRIPTION>\n",
    "        Midpoint of the spectral coverage in Angstroms\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <PARAM ID=\"SpectralBoundsExtent\" datatype=\"double\" name=\"OUTPUT:SpectralBoundsExtent\" ucd=\"instr.bandwidth\" unit=\"AA\" utype=\"ssa:Char.Spectral.Coverage.Bounds.Extent\" value=\"0\">\n",
    "       <DESCRIPTION>\n",
    "        Width of spectrum in Angstroms\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <PARAM ID=\"SpectralBoundsStart\" datatype=\"double\" name=\"OUTPUT:SpectralBoundsStart\" ucd=\"em.wl;stat.min\" unit=\"AA\" utype=\"ssa:Char.Spectral.Coverage.Bounds.Start\" value=\"0\">\n",
    "       <DESCRIPTION>\n",
    "        Start of spectral coordinate in Angstroms\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <PARAM ID=\"SpectralBoundsStop\" datatype=\"double\" name=\"OUTPUT:SpectralBoundsStop\" ucd=\"em.wl;stat.max\" unit=\"AA\" utype=\"ssa:Char.Spectral.Coverage.Bounds.Stop\" value=\"0\">\n",
    "       <DESCRIPTION>\n",
    "        End of spectral coordinate in Angstroms\n",
    "       </DESCRIPTION>\n",
    "      </PARAM>\n",
    "      <INFO ID=\"QUERY_STATUS\" name=\"QUERY_STATUS\" value=\"OK\"/>\n",
    "      <INFO ID=\"SERVICE_PROTOCOL\" name=\"SERVICE_PROTOCOL\" value=\"1.1\"/>\n",
    "     </RESOURCE>\n",
    "    </VOTABLE>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
